﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Utility;
using eShop.Business;
using eShop.Model;
using ECommerce.Library;

public partial class controls_product_list : BaseControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public override void DataBind()
    {
        rPrducts.DataBind();
        if (this.ShowPaging)
        {
            pnlPaging.Visible = true;
            int firstItemPosition = (this.SelectedPageIndex - 1) * this.PageSize;
            int lastItemPosition = this.SelectedPageIndex * this.PageSize - 1;

            if (lastItemPosition >= this.TotalItemCount)
                lastItemPosition = this.TotalItemCount - 1;


            lblTotalItem.Text = TotalItemCount.ToString();
            lblFirstItemNumber.Text = this.TotalItemCount == 0 ? "0" : (firstItemPosition + 1).ToString();
            lblLastItemNumber.Text = (lastItemPosition + 1).ToString();
            BindPager();
        }
    }

    protected void rPrducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Product product = (Product)e.Item.DataItem;
            Image imgProduct = (Image)e.Item.FindControl("imgProduct");
            imgProduct.ImageUrl = ImageResizer.CheckImageMissing(product.ProductImageSmallThumbnail, eShop.Business.SiteSettings.NoImageSmallThumbnailUrl);
        }
    }

    public List<Product> DataSource
    {
        get
        {
            if (rPrducts.DataSource == null)
                return null;
            return (List<Product>)rPrducts.DataSource;
        }
        set
        {
            rPrducts.DataSource = value;
        }
    }

    public bool ShowPaging { get; set; }
    public int SelectedPageIndex { get; set; }
    public int PageSize { get; set; }
    public int TotalItemCount { get; set; }
    public int TotalPages 
    {
        get
        {
            return (int)Math.Ceiling(TotalItemCount / (double)PageSize);
        }
    }

    public event PageIndexChangedHandler PageIndexChanged;

    public delegate void PageIndexChangedHandler(int selectedPageIndex);



    #region Paging

    protected void EnableLinkButtonStyle(LinkButton lkb)
    {
        lkb.CssClass = "paging-numbers";
        lkb.OnClientClick = "javascript:return true;";
    }

    protected void DisableLinkButtonStyle(LinkButton lkb)
    {

        lkb.CssClass = "paging-numbers current-page";
        lkb.OnClientClick = "javascript:return false;";
    }

    private void BindPager()
    {
        List<string> pages = new List<string>();
        for (int i = 1; i <= this.TotalPages; i++)
            pages.Add(i.ToString());

        rptPaging.DataSource = pages;
        rptPaging.DataBind();
    }

    protected void lkbPrev_Click(object sender, EventArgs e)
    {
        try
        {
            
            SelectedPageIndex = SelectedPageIndex > 1 ? SelectedPageIndex - 1 : 1;
            DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptPaging_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int pageIndex =Convert.ToInt32(e.Item.DataItem.ToString());
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("hlkPage");
                lkbPageIndex.Text = pageIndex.ToString();

                if (SelectedPageIndex == pageIndex)
                {
                    DisableLinkButtonStyle(lkbPageIndex);
                }
                else
                {
                    EnableLinkButtonStyle(lkbPageIndex);
                    lkbPageIndex.CommandArgument = pageIndex.ToString();
                }
               
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            SelectedPageIndex = int.Parse(e.CommandArgument.ToString());
            DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void lkbNext_Click(object sender, EventArgs e)
    {
        try
        {
            SelectedPageIndex = SelectedPageIndex < this.TotalPages ? SelectedPageIndex + 1 : this.TotalPages;
            lblFirstItemNumber.Text = SelectedPageIndex.ToString();
            DataBind();
            //if (TotalPages > 1)
            //{
            //    lkbPrev.Visible = true;
            //}
            //else
            //{
            //    lkbPrev.Visible = false;
            //}
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
   
    #endregion
}